<template>
  <div class="mod-config">
    <div v-if="studyDetailVisable">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()">
        <el-form-item label="日期和时间范围：">
          <el-date-picker
            style="width:220px;"
            v-model="dataForm.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="开始时间"
          ></el-date-picker>
          ~
          <el-date-picker
            style="width:220px;"
            v-model="dataForm.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="结束时间"
          ></el-date-picker>
          <el-button @click="search()">查询</el-button>
          <el-button type="primary" @click="goback()">返回</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%;"
      >
        <el-table-column prop="gmtCreated" header-align="center" align="center" label="日期"></el-table-column>
        <el-table-column prop="id" header-align="center" align="center" label="笔记id"></el-table-column>
        <el-table-column header-align="center" align="center" label="笔记内容">
          <template slot-scope="scope">
            <div @mouseover="showNote(scope.row.noteContent)" class="text-el" v-html="noteHandleList(scope.row.noteContent)"></div>
          </template>
        </el-table-column>
        <el-table-column header-align="center" align="center" label="查重结果">
          <template slot-scope="scope">
            {{ scope.row.repetitionStatus === 1?'通过':'' }}
            {{ scope.row.repetitionStatus === 2?'不通过':'' }}
          </template>
        </el-table-column>
        <el-table-column header-align="center" align="center" label="是否分享">
          <template slot-scope="scope">
            {{ scope.row.isShare === 1?'是':'' }}
            {{ scope.row.isShare === 0?'否':'' }}
          </template>
        </el-table-column>
        <el-table-column header-align="center" align="center" label="有效笔记日任务">
          <template slot-scope="scope">
            <span>{{ scope.row.repetitionStatus === 1 && scope.row.isShare === 1 ? '有效' : '无效' }}</span>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>
    </div>
    <!--笔记预览-->
    <div v-if="shownote" class="preview" @mouseleave="hideNote">
      <div v-html="noteHtml"></div>
    </div>
  </div>
</template>

<script>

  export default {
    data() {
      return {
        shownote: false,
        studyDetailVisable: true,
        id: '',
        isAdd: true,
        visible1: false,
        member: true,
        dataForm1: {
          isFinishPlan: ''
        },
        dataForm: {
          reviewState: null,
          isPublic: '1',
          label: null
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        showDatePicker: false,
        readingTime: ''
      }
    },
    watch: {
      // 如果 `question` 发生改变，这个函数就会运行
      readingTime: function (newQuestion, oldQuestion) {
        this.dataForm1.readingTime = newQuestion
        if (newQuestion >= 20) {
          this.dataForm1.isFinishPlan = 1
        } else {
          this.dataForm1.isFinishPlan = 0
        }
      }
    },
    mounted() {
    },
    activated() {
      this.getDataList()
    },
    methods: {
      search() {
        this.pageIndex = 1
        this.getDataList()
      },
      showNote(note) {
        this.shownote = true
        this.noteHtml = this.noteHandle(note)
      },
      noteHandle(note) {
        if (note) {
          let noteHtml = ''
          let noteArr = JSON.parse(note)
          noteArr.map((item) => {
            if (item.type === 'text') {
              noteHtml += item.content + '<br>'
            } else if (item.type === 'image') {
              noteHtml += `<img src="${item.content}" style="width: 300px;margin: 15px 0;">` + '<br>'
            }
          })
          return noteHtml
        }
      },
      noteHandleList(note) {
        if (note) {
          let noteHtml = ''
          let noteArr = JSON.parse(note)
          noteArr.map((item) => {
            if (item.type === 'text') {
              noteHtml += item.content
            } else if (item.type === 'image') {
              // noteHtml += `<img src="${item.content}" style="width: 100%;margin: 15px 0;"` + '<br>'
            }
          })
          return noteHtml
        }
      },
      hideNote() {
        this.shownote = false
      },
      // 获取数据列表
      getDataList(id) {
        if (id) {
          this.id = id
        }
        // this.dataListLoading = true
        let obj = {
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key
        };
        obj.userId = id || this.id
        obj.startTime = this.dataForm.startTime;
        obj.endTime = this.dataForm.endTime;
        this.$http({
          url: this.$http.adornUrl('/course/noterecord/getNoteDesc'),
          method: 'get',
          params: this.$http.adornParams(obj)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      look(id, statTime) {
        this.studyDetailVisable = false
        this.$nextTick(() => {
          this.$refs.studyRecordDetail.getDataList(id, statTime)
        })
      },
      showMember() {
        this.studyDetailVisable = true
      },
      // 新增 / 修改
      modify(data) {
        if (data) {
          this.visible1 = true
          this.isAdd = false
          this.dataForm1 = data
        } else {
          this.visible1 = true
          this.isAdd = true
          this.dataForm1 = {
            isFinishPlan: ''
          }
        }
      },
      submitData() {
        const that = this
        let obj = {
          userId: that.id,
          statTime: that.dataForm1.statTime,
          id: that.dataForm1.id || '',
          isFinishPlan: that.dataForm1.isFinishPlan,
          readingTime: that.dataForm1.readingTime * 60
        }
        this.$http({
          url: this.$http.adornUrl(
            `/course/studydurationstatistics/${this.dataForm1.id ? 'update' : 'save'}`
          ),
          method: 'post',
          data: this.$http.adornData(obj)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible1 = false
                this.getDataList()
              }
            });
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      goback() {
        this.$emit('goBack')
      }
    }
  }
</script>
<style>
  .margin_10 {
    margin-bottom: 10px;
  }

  .text_r {
    text-align: right;
    padding-right: 15px;
  }

  .inineBlock {
    display: inline-block;
  }

  img {
    width: 100%;
    height: auto;
  }

  .imgList {
    display: flex;
    flex-wrap: wrap;
  }

  .imgList > div {
    width: 80px;
    margin: 5px 15px 5px 0;
  }
  .text-el{
    cursor: pointer;
    width: 180px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow:ellipsis;
  }
  .preview{
    position: fixed;
    padding: 20px;
    width: 42%;
    height: 70%;
    left: 20%;
    top: 150px;
    z-index: 99999;
    background: #fff;
    border: 1px solid #000000;
    border-radius: 4px;
    overflow: auto;
    line-height: 22px;
    font-size: 15px;
  }
</style>
